package pool

import (
	"github.com/gin-gonic/gin"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

type Metrics struct {
	jobTotalCounter *prometheus.CounterVec // job total counter
	executingGauge *prometheus.GaugeVec
	namespace string
}

func NewMetrics(namespace string) *Metrics {
	m := &Metrics{
		jobTotalCounter: prometheus.NewCounterVec(prometheus.CounterOpts{
			Namespace: namespace,
			Name: "jobTotal",
			Help:      "统计运行job总量",
		}, []string{"jobTotal"}),
		executingGauge: prometheus.NewGaugeVec(prometheus.GaugeOpts{
			Namespace:namespace,
			Name: "executing",
			Help:      "统计正在执行的job数量",
		},[]string{"executing"}),
	}
	prometheus.MustRegister(m.jobTotalCounter,m.executingGauge)

	return m
}

// job total ++
func (m *Metrics) JobTotalInc()  {
	m.jobTotalCounter.WithLabelValues("jobTotal").Inc()
}

// job executing ++
func (m *Metrics) JobExecutingInc(v int)  {
	m.executingGauge.WithLabelValues("jobTotal").Add(float64(v))
}

func (m *Metrics) ginHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		promhttp.Handler().ServeHTTP(c.Writer,c.Request)
	}
}